using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;

namespace Game.Core
{
    public unsafe partial class MovementSystem : SystemBase
    {
        protected override void OnCreate()
        {
            InitMovableQuery();
        }

        protected override void OnUpdate()
        {
            Dependency = ScheduleMovableJob(Dependency);
        }

        #region Movable

        private EntityQuery m_MovableQuery;
        protected void InitMovableQuery()
        {
            var builder = new EntityQueryBuilder(Allocator.Temp);
            builder.AddAll(ComponentType.ReadOnly<Movable>());
            builder.AddAll(ComponentType.ReadWrite<LocalTransform>());
            // builder.FinalizeQuery();
            m_MovableQuery = GetEntityQuery(builder);
            builder.Dispose();
        }

        protected JobHandle ScheduleMovableJob(JobHandle handle)
        {
            var job = new MoveJob();
            job.MoveTime = World.Time.DeltaTime;
            handle = job.ScheduleParallel(m_MovableQuery, handle);
            return handle;
        }

        #endregion
    }
}